<template>
  <div class="main">
    <div class="container">
      <div class="container-top">
        <div class="top">
          <div class="title">申报用户管理</div>
        </div>
        <div class="contentTitle">
          <p>修改用户</p>
        </div>
      </div>
      <div class="content">
        <el-form ref="form" :model="form" label-width="160px" :rules="rules">
          <el-form-item label="账号">
            <el-input
              v-model="form.user_name"
              class="common"
              placeholder="单行输入"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model="form.password"
              autocomplete="off"
              show-password
              class="common"
              placeholder="单行输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="确定密码" prop="checkPass">
            <el-input
              type="password"
              v-model="form.checkPass"
              autocomplete="off"
              show-password
              class="common"
              placeholder="单行输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="real_name">
            <el-input
              v-model="form.real_name"
              class="common"
              placeholder="单行输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="所属院系/部门" prop="yx_name">
            <el-input
              v-model="form.yx_name"
              class="common"
              placeholder="单行输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-select v-model="form.sex" placeholder="请选择" class="common">
              <el-option label="男" value="1"></el-option>
              <el-option label="女" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="手机" prop="phone">
            <el-input
              v-model="form.phone"
              class="common"
              placeholder="单行输入"
            ></el-input>
          </el-form-item>
          <el-form-item class="button">
            <el-button icon="iconfont icon-guanbi" class="close" @click="back"
              >取消</el-button
            >
            <el-button
              type="primary"
              @click="onSubmit('form')"
              icon="iconfont icon-baocun_mian"
              class="save"
              >保存</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import { UserReportad, UserModify } from "@/api/login";
import regular from "@/utils/regular.js";
export default {
  name: "ModifyUser",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.form.checkPass !== "") {
          this.$refs.form.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      Showbg: false,
      accountValue: "16502360259",
      form: {
        user_name: "",
        password: "",
        checkPass: "",
        real_name: "",
        yx_name: "",
        // bm_name: "",
        sex: "男",
        phone: "",
      },
      rules: {
        real_name: [{ validator: regular.validatorName, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        // yx_name: [{ validator: regular.validatorCollege, trigger: "blur" }],
        // bm_name: [
        //   { validator: regular.validatorDepartment, trigger: "blur" },
        // ],
        // phone: [
        //   {
        //     validator: regular.checkMobile,
        //     trigger: "blur",
        //   },
        // ],
      },
      dialogImageUrl: "",
      dialogVisible: false,
      fileList: [],
      fileType: "",
      modifyData: [],
      userid: "",
      token: "",
    };
  },
  filters: {
    filterType(val) {
      if (val === 0) return "0 B";
      var k = 1024, // or 1024
        sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"],
        i = Math.floor(Math.log(val) / Math.log(k));
      return (val / Math.pow(k, i)).toPrecision(3) + " " + sizes[i];
    },
  },
  methods: {
    handleChange(file, fileList) {
      this.fileType = file.raw.type.split("/")[1];
      if (fileList.length > 0) {
        this.fileList = [fileList[fileList.length - 1]];
      }
    },
    handleRemove(file, fileList) {
      this.fileList = [];
    },
    handlePreview(file, fileList) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //  点击提交
    onSubmit(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.queryUserReportad();
        } else {
          return false;
        }
      });
    },
    queryUserModify() {
      UserModify({
        token: this.token,
        user_id: this.userid,
      }).then((result) => {
        const { data } = result;
        this.form = data;
      });
    },
    queryUserReportad() {
      UserReportad({
        token: this.token,
        user_id: this.userid,
        user_name: this.form.user_name,
        pwd1: this.form.password,
        pwd2: this.form.checkPass,
        real_name: this.form.real_name,
        yx_name: this.form.yx_name,
        // bm_name: this.form.bm_name,
        sex: this.form.sex,
        phone: this.form.phone,
        avatar: this.fileList.url,
      }).then((result) => {
        if (result.code === 1) {
          this.$message({
            message: "保存成功",
            type: "success",
          });
          this.$router.push({
            path: "/user-management",
          });
        }
      });
    },
    back() {
      this.$router.go(-1);
    },
    backgroundShow() {
      this.$emit("backgroundShow", this.Showbg);
    },
  },
  created() {
    this.backgroundShow();
  },
  mounted() {
    const token = sessionStorage.getItem("token");
    const userid = this.$route.query.userid;
    this.token = token;
    this.userid = userid;
    this.queryUserModify();
  },
};
</script>
<style lang='scss' scoped>
.main {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  padding: 0;
  color: #242748;
  margin: 0 auto;
  position: relative;
  font-size: 18px;
  margin: 0 50px 20px 30px;
  text-align: left;
  font-family: "PingFang";
  .container {
    width: 100%;
    height: 925px;
    overflow-y: auto;
    background-color: #fff;
    border-radius: 10px;
    .container-top {
      .top {
        display: flex;
        margin-bottom: 30px;
        .title {
          font-size: 26px;
          font-family: "黑体";
          color: #5673ff;
          margin-right: 60px;
        }
      }
      .contentTitle {
        font-size: 22px;
        color: #242748;
        line-height: 32px;
        font-weight: 700;
        p {
          font-weight: 700;
        }
      }
    }
    .container-top {
      padding: 33px 45px 25px;
      border-bottom: solid 1px #e7e9eb;
    }
    .content {
      padding: 40px 40px 0;
      .common {
        width: calc(100% - 40%);
      }
      /deep/.el-input__inner {
        font-size: 18px;
        color: #9d9fa2;
        font-family: "PingFang";
      }
      /deep/.el-input.is-disabled .el-input__inner {
        background-color: #f4f7fc;
      }
      /deep/.el-form-item {
        margin-bottom: 38px;
      }
      /deep/.el-form-item__label {
        text-align: left;
        color: #242748;
        font-size: 18px;
        font-weight: 700;
      }
      /deep/.button .el-form-item__content {
        margin-left: 0 !important;
      }
      /deep/.el-upload {
        float: left;
      }
      /deep/.el-upload__tip {
        font-size: 18px !important;
        color: #242748 !important;
      }
      /deep/.el-upload-list {
        width: 35%;
      }
      /deep/.el-upload-list--picture .el-upload-list__item {
        height: 70px;
        line-height: 70px;
        background-color: #f4f7fc;
        border: none;
      }
      /deep/.el-upload-list__item-thumbnail {
        width: 30px;
        height: 30px;
      }
      /deep/.el-upload-list--picture
        .el-upload-list__item.is-success
        .el-upload-list__item-name {
        line-height: 48px;
        color: #242748;
        font-size: 14px;
        font-weight: 700;
      }
      .uploadBtn {
        padding: 7px 12px 7px 10px;
        margin-right: 10px;
        background-color: #e5eefe;
        border: solid 1px #c8e3fe;
        color: #2197fd;
        font-size: 14px;
        letter-spacing: 1px;
      }
      /deep/.el-icon-circle-plus {
        color: #2197fd;
        font-size: 16px;
      }
      .preview {
        display: flex;
        align-items: center;
        width: 100%;
        height: 50px;
        padding: 0 22px;
        background-color: #f4f7fc;
        border-radius: 5px;
        .details {
          width: 80%;
          margin: 0 0 0 20px;
          .title {
            width: 90%;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            color: #242748;
            font-size: 14px;
            font-weight: 700;
          }
          p {
            line-height: 20px;
            font-size: 12px;
            color: #a1a4b5;
            span {
              margin-right: 10px;
            }
          }
        }
        .preview-btn {
          height: 18px;
          display: flex;
          & > div {
            width: 18px;
            height: 18px;
            margin-left: 18px;
            img {
              display: block;
            }
          }
          .del {
            width: 16px;
          }
        }
      }
      .save {
        background-color: #5673ff;
        border: solid 1px #2d4eff;
        box-shadow: 0px 4px 15.52px 0.48px rgba(73, 100, 245, 0.39);
      }
      .close {
        border: solid 1px #e7e9eb;
        background-color: rgb(255, 255, 255);
        box-shadow: 0px 4px 15.52px 0.48px rgba(73, 100, 245, 0.14);
        color: #242748;
      }
      .save,
      .close {
        padding: 8px 15px 8px 20px;
        font-size: 14px;
        font-family: "PingFang";
        letter-spacing: 1px;
        margin-right: 10px;
        /deep/i {
          margin-right: 8px;
          font-size: 18px;
          vertical-align: middle;
        }
      }
    }
  }
}
</style>